package sz.szsmk.citizencard.mvp.presenter;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ProtocolsAgreeReq;
import sz.szsmk.citizencard.model.network.bean.request.RegisterRequest;
import sz.szsmk.citizencard.model.network.bean.request.ResetPsdReq;
import sz.szsmk.citizencard.model.network.bean.request.SetPsdRequest;
import sz.szsmk.citizencard.model.network.bean.request.ThirdAppLoginReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.ThirdAppLoginRes;
import sz.szsmk.citizencard.model.network.bean.response.UserBean;
import sz.szsmk.citizencard.mvp.contract.IMainContact;
import sz.szsmk.citizencard.mvp.contract.ISetPsdContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.SineUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/4 10:23
 * description:
 */
public class SetPsdPresenter extends IBasePresenter
        implements ISetPsdContact.ISetPsdPresenter<ISetPsdContact.ISetPsdView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SetPsdPresenter(GsdxNetApi gsdxNetApi) {
        this.mNetworkDateSource = NetworkDateSources.getInstance();
        this.gsdxNetApi = gsdxNetApi;
    }

    @Override
    public void register(String mobile, String setPsdEtSure, String sms_code) {

        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        registerRequest.setMobile(mobile);
        registerRequest.setPassword(setPsdEtSure);
        registerRequest.setSms_code(sms_code);
        mNetworkDateSource.register(registerRequest, new SubscribeResult<UserBean>() {
            @Override
            protected void onOk(UserBean response) {
                ACache.get(DaggerApplication.getIns()).put("access_token",response.getAccess_token());
                ACache.get(DaggerApplication.getIns()).put("refresh_token",response.getRefresh_token());
                ((ISetPsdContact.ISetPsdView) mView).registerSuccess(response);
            }

            @Override
            protected void onFailure(UserBean response) {
                super.onFailure(response);
                ((ISetPsdContact.ISetPsdView) mView).registerFail(response.getMessage());
            }

            @Override
            protected void onNext(UserBean response) {
                super.onNext(response);
                ((ISetPsdContact.ISetPsdView) mView).registerFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISetPsdContact.ISetPsdView) mView).registerFail(throwable.getMessage());

            }
        });

    }

    @Override
    public void modifyPsd(String setPsdEtSure, String oldPsd) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");

        String refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token");

        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());
        if (access_token == null) {
            return;
        }
        ResetPsdReq resetPsdReq = new ResetPsdReq();
        resetPsdReq.setAccess_token(access_token);
        resetPsdReq.setDevice_id(androidId);
        resetPsdReq.setNewPassWord(setPsdEtSure);
        resetPsdReq.setOldPassWord(oldPsd);
        SineUtils.setSine(resetPsdReq, access_token, androidId, setPsdEtSure, oldPsd, refresh_token);
//        String key = "access_token=" + access_token + "&device_id=" + androidId + "&key=" + refresh_token;
//        String stringMd5 = MD5Utils.getStringMd5(key).toUpperCase();
//        resetPsdReq.setSign(stringMd5);
        mNetworkDateSource.resetPassWord(resetPsdReq, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String code = response.getCode();
                ((ISetPsdContact.ISetPsdView) mView).modifyPsdSuccess();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
                ((ISetPsdContact.ISetPsdView) mView).modifyPsdFail(response.getMessage());
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((ISetPsdContact.ISetPsdView) mView).modifyPsdFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISetPsdContact.ISetPsdView) mView).modifyPsdFail(throwable.getMessage());

            }
        });

    }

    @Override
    public void forgetPsd(String mobile, String setPsdEtSure,String sms_code,String sms_type) {
        SetPsdRequest setPsdRequest = new SetPsdRequest();
        setPsdRequest.setPassWord(setPsdEtSure);
        setPsdRequest.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        setPsdRequest.setMobile(mobile);
        setPsdRequest.setSms_code(sms_code);
        setPsdRequest.setSms_type(sms_type);
        String json = new Gson().toJson(setPsdRequest);
        L.e(Constant.TAG, "forgetPassWord=====>" + json);
        mNetworkDateSource.setUserPad(setPsdRequest, new SubscribeResult<IbaseBean>() {
            @Override
            public void call(IbaseBean ibaseBean) {
//                SharedPreferencesTool.saveBoolean(DaggerApplication.getIns(), Constant.IS_LOGIN, true);
                Toast.makeText(DaggerApplication.getIns(), ibaseBean.getMessage(), Toast.LENGTH_SHORT).show();
                if (ibaseBean.getCode().equals("000000")) {
                    ((ISetPsdContact.ISetPsdView) mView).forgetPsdSucc();
                } else {

                    ((ISetPsdContact.ISetPsdView) mView).forgetPsdFail();
                }
            }

            @Override
            protected void onOk(IbaseBean response) {
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
                ((ISetPsdContact.ISetPsdView) mView).forgetPsdSucc();
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                Toast.makeText(DaggerApplication.getIns(), throwable.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void thirdAppLogin(int login_type, String mobile, String setPsdEtSure, String uid) {
        ThirdAppLoginReq thirdAppLoginReq = new ThirdAppLoginReq();
        thirdAppLoginReq.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        thirdAppLoginReq.setLogin_type(login_type);
        thirdAppLoginReq.setMobile(mobile);
        thirdAppLoginReq.setPassword(setPsdEtSure);
        thirdAppLoginReq.setUid(uid);
        String json = new Gson().toJson(thirdAppLoginReq);
        L.e(Constant.TAG, "thirdAppLogin===req：" + json);
        mNetworkDateSource.thirdAppLogin(thirdAppLoginReq, new SubscribeResult<ThirdAppLoginRes>() {
            @Override
            protected void onOk(ThirdAppLoginRes response) {

                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "thirdAppLogin===res：" + json);
                ACache.get(DaggerApplication.getIns()).remove("login_type");
                ACache.get(DaggerApplication.getIns()).remove("uid");
                String access_token = response.getAccess_token();
                ACache.get(DaggerApplication.getIns()).remove("access_token");
                ACache.get(DaggerApplication.getIns()).remove("refresh_token");
                ACache.get(DaggerApplication.getIns()).put("access_token", access_token);
                ACache.get(DaggerApplication.getIns()).put("refresh_token", response.getRefresh_token());
                ((ISetPsdContact.ISetPsdView) mView).thirdAppLoginSucc();
            }

            @Override
            protected void onFailure(ThirdAppLoginRes response) {
                super.onFailure(response);

                ACache.get(DaggerApplication.getIns()).remove("login_type");
                ACache.get(DaggerApplication.getIns()).remove("uid");
            }

            @Override
            protected void onNext(ThirdAppLoginRes response) {
                super.onNext(response);

                ACache.get(DaggerApplication.getIns()).remove("login_type");
                ACache.get(DaggerApplication.getIns()).remove("uid");
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                ACache.get(DaggerApplication.getIns()).remove("login_type");
                ACache.get(DaggerApplication.getIns()).remove("uid");

            }
        });
    }

    @Override
    public void addUserAppAgreementRecord(String clientVersion, String operateSystem, String sceneNo,String token) {
        ProtocolsAgreeReq protocolsAgreeReq = new ProtocolsAgreeReq();
        protocolsAgreeReq.setAccess_token(token);
        protocolsAgreeReq.setClientVersion(clientVersion);
        protocolsAgreeReq.setOperateSystem(operateSystem);
        protocolsAgreeReq.setSceneNo(sceneNo);
        mNetworkDateSource.addUserAppAgreementRecord(protocolsAgreeReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((ISetPsdContact.ISetPsdView) mView).addUserAppAgreementRecordSucc(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISetPsdContact.ISetPsdView) mView).addUserAppAgreementRecordFail();
            }
        });
    }

}
